/**
 * 日志清理定时任务
 * 作者：GodMainCode
 * 创建时间：2024-01-17
 * 修改时间：2024-01-17
 * 修改人：GodMainCode
 */

import { Injectable, Logger } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { LogService } from './log.service';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class LogSchedule {
  private readonly logger = new Logger(LogSchedule.name);

  constructor(
    private readonly logService: LogService,
    private readonly configService: ConfigService,
  ) {}

  /**
   * 每天凌晨3点执行日志清理
   */
  @Cron(CronExpression.EVERY_DAY_AT_3AM)
  async handleLogClean() {
    try {
      const days = this.configService.get<number>('LOG_RETENTION_DAYS', 30);
      await this.logService.clean(days);
      this.logger.log(`Successfully cleaned logs older than ${days} days`);
    } catch (error) {
      this.logger.error('Failed to clean logs:', error);
    }
  }
} 